package com.luyun.frogpay.base.application;

import android.app.Application;
import android.content.Context;
import android.os.RemoteException;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;

import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechUtility;
import com.luyun.frogpay.base.baseclass.ActivityManager;
import com.luyun.frogpay.base.dagger.app.component.ApplicationComponent;
import com.luyun.frogpay.base.dagger.app.component.DaggerApplicationComponent;
import com.luyun.frogpay.base.dagger.app.module.ApplicationModule;
import com.luyun.frogpay.base.global.EnumWxFacePayPublicErrorCode;
import com.luyun.frogpay.base.utils.Log;
import com.luyun.frogpay.base.utils.ScreenAdapter;
import com.luyun.frogpay.base.utils.TTSUtils;
import com.orhanobut.hawk.Hawk;
import com.tencent.wxpayface.IWxPayfaceCallback;
import com.tencent.wxpayface.WxPayFace;

import java.util.Map;

import static com.luyun.frogpay.base.global.WxPayFaceSDKParam.RETURN_CODE;
import static com.luyun.frogpay.base.global.WxPayFaceSDKParam.RETURN_MSG;


public class BaseApplication extends Application {

    public static BaseApplication instance;
    public static Context context;
    private ApplicationComponent applicationComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        SpeechUtility.createUtility(BaseApplication.this, SpeechConstant.APPID + "=5df843c3"); //初始化
        context = this.getApplicationContext();
        instance = this;
        initScreenAdapter();
        initLog();
        initUtils();
        initApplicationComponent();
        initARouter();
        initFaceSDK();
        initHawk();
        TTSUtils.init(context);
    }

    /**
     * 初始化数据存储
     */
    private void initHawk() {
        Hawk.init(context).build();
    }

    /**
     * 初始化微信刷脸SDK
     */
    private void initFaceSDK() {
        WxPayFace.getInstance().initWxpayface(context, new IWxPayfaceCallback() {
            @Override
            public void response(Map map) throws RemoteException {
                Log.d("initWxpayface: " + map.toString());
                String returnCode = (String) map.get(RETURN_CODE);
                String returnMsg = (String) map.get(RETURN_MSG);
                if (String.valueOf(EnumWxFacePayPublicErrorCode.SYSTEMERROR).equals(returnCode)){
                    ToastUtils.showShort("系统异常，请重新打开应用重试");
                }else if (String.valueOf(EnumWxFacePayPublicErrorCode.SUCCESS).equals(returnCode)){

                }else {
                    ToastUtils.showShort(returnMsg);
                }
            }
        });
    }

    /**
     * 初始化阿里路由
     */
    private void initARouter() {
        // 这两行必须写在init之前，否则这些配置在init过程中将无效
        // 打印日志
        ARouter.openLog();
        // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
        ARouter.openDebug();
        // 尽可能早，推荐在Application中初始化
        ARouter.init(this);
    }

    /**
     * 屏幕适配
     */
    private void initScreenAdapter() {
        ScreenAdapter.setup(this);
        ScreenAdapter.register(this, 360, ScreenAdapter.MATCH_BASE_WIDTH, ScreenAdapter.MATCH_UNIT_DP);
    }

    /**
     * 初始化日志框架
     */
    private void initLog() {
        Log.init(true);
    }

    /**
     * 初始化 utilcode
     */
    private void initUtils() {
        Utils.init(this);
    }

    /**
     * 初始化 applicationComponent
     */
    private void initApplicationComponent(){
        applicationComponent = DaggerApplicationComponent
                .builder()
                .applicationModule(new ApplicationModule(this))
                .build();
    }

    public ApplicationComponent getApplicationComponent(){
        return applicationComponent;
    }
}
